package com.icss.ah.productEval.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.productEval.bean.ProductEvalBean;
import com.icss.ah.productEval.bean.ProductInfoBean;
import com.icss.ah.productEval.bean.ProductReplyEvalBean;


@Service
@Transactional
public class ProductEvalService {

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	/**
	 * 获取评价商品列表
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getProductEvalList(Map<String, String> map) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = sqlSessionTemplate.selectList(
					"ProductEvalBean.getProductEvalList", map);
			addScoreStar(list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalService|getProductEvalList", e);
		}
		return list;
	}
	
	/**
	 * 增加五星图标展示评价得分
	 * @param list
	 * @return
	 */
	private void addScoreStar(List<Map<String, Object>> list){
		for(Map<String, Object> map:list){
			StringBuffer starImg=new StringBuffer("");
			starImg.append("<div class='stars_item'>");
			starImg.append("	<ul>");
			Double score=((BigDecimal) map.get("evalScore")).doubleValue();
			for(int i=0;i<Math.floor(score);i++){//满背景星星
				starImg.append("<li style='background-position:0,-px'></li>");
			}
			if(score==Math.floor(score) && score<5){
				starImg.append("<li class='star_n'></li>");
			}else if(score<5){
				starImg.append("<li class='star_f'></li>");
			}
			if(score<4){
				for(int i=1;i<(5-Math.floor(score));i++){//无背景星星
					starImg.append("<li class='star_n'></li>");
				}
			}
			starImg.append("</ul>");
			starImg.append("</div>");
			
			map.put("starImg", starImg.toString());
			
		}
		
	}

	/**
	 * 获取商品评论总数
	 * @param map
	 * @return
	 */
	public Long getProductEvalTotal(Map<String, String> map) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Long count = 0L;
		try {
			list = sqlSessionTemplate.selectList("ProductEvalBean.getProductEvalTotal",map);
			count=(long) list.size();
			infolog.info("ProductEvalService|getProductEvalTotal|count: "+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalService|getProductEvalTotal", e);
		}
		return count;
	}

	/**
	 * 根据id获取商品信息
	 * @param map
	 * @return
	 */
	public ProductInfoBean getProductById(Map<String, String> map) {
		ProductInfoBean productInfo=new ProductInfoBean();
		try {
			productInfo=sqlSessionTemplate.selectOne("ProductEvalBean.getProductById", map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalService|getProductById", e);
		}
		return productInfo;
	}

	/**
	 * 获取评价列表
	 * @param map
	 * @return
	 */
	public List<ProductEvalBean> getEvalList(Map<String, String> map) {
		List<ProductEvalBean> evalList = new ArrayList<ProductEvalBean>();
		try {
			evalList = sqlSessionTemplate.selectList(
					"ProductEvalBean.getEvalList", map);
			List<ProductReplyEvalBean> replyToEvalList = getReplyToEval(map);
			List<ProductReplyEvalBean> replyToReplyList = getReplyToReply(map);
			
			choseReplyToReply(replyToEvalList,replyToReplyList);
			choseReplyToEval(evalList,replyToEvalList);
			
			
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalService|getEvalList", e);
		}
		return evalList;
	}
	
	/**
	 * 根据评价id 把对应回复归入评价
	 * @param evalList 所有评价列表
	 * @param replyList 所有回复列表
	 */
	private void choseReplyToEval(List<ProductEvalBean> evalList,List<ProductReplyEvalBean> replyList){
		for(ProductEvalBean eval:evalList){
			for(ProductReplyEvalBean reply:replyList){
				if(eval.getResultId().equals(reply.getResultId())){//匹配评价id 填充回复列表
					eval.getReplylist().add(reply);
				}
			}
		}
	}
	
	/**
	 * 根据回复id 把对应回复归入 回复的回复列表
	 * @param replyList 回复列表
	 * @param replyToReplyList 回复回复的列表
	 */
	private void choseReplyToReply(List<ProductReplyEvalBean> replyList,List<ProductReplyEvalBean> replyToReplyList){
		for(ProductReplyEvalBean reply:replyList){
			for(ProductReplyEvalBean replyToReply:replyToReplyList){
				if(reply.getReplyId().equals(replyToReply.getReplyTarget())){
					reply.getReplyToReplyList().add(replyToReply);
				}
			}
			if(reply.getReplyToReplyList().size()>0){
				choseReplyToReply(reply.getReplyToReplyList(),replyToReplyList);
			}
		}
	}
	
	/**
	 * 获取所有针对评价的回复
	 * @return
	 */
	private List<ProductReplyEvalBean> getReplyToEval(Map<String, String> map){
		List<ProductReplyEvalBean> replylist = new ArrayList<ProductReplyEvalBean>();
		try {
			replylist = sqlSessionTemplate.selectList(
					"ProductEvalBean.getReplyToEval",map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalService|getReplyToEval", e);
		}
		
		return replylist;
	}
	
	/**
	 * 获取所有针对回复的回复
	 * @return
	 */
	private List<ProductReplyEvalBean> getReplyToReply(Map<String, String> map){
		List<ProductReplyEvalBean> replylist = new ArrayList<ProductReplyEvalBean>();
		try {
			replylist = sqlSessionTemplate.selectList(
					"ProductEvalBean.getReplyToReply",map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalService|getReplyToReply", e);
		}
		
		return replylist;
	}

	/**
	 * 获取评价总数
	 * @param map
	 * @return
	 */
	public Long getEvalTotal(Map<String, String> map) {
		Long count = 0L;
		try {
			count = sqlSessionTemplate.selectOne("ProductEvalBean.getEvalTotal",map);
			infolog.info("ProductEvalService|getEvalTotal|count: "+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductEvalService|getEvalTotal", e);
		}
		return count;
	}
	
	
}
